package com.xunta.common.utils;

import com.xunta.SpringBootXunTaApplication;
import io.netty.util.internal.StringUtil;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipOutputStream;

/**
 * @author 不愿透露
 * @Classname D
 * @Description TODO
 * @Date 2021/7/29 10:51
 */
public class JdbcConfig {
    private static final String USE = "xun_ta";

    /*Map<String, String> table = new HashMap<>();
        table.put("tableName","sys_test");
        table.put("tableComment","测试表");
    List<Map<String, String>> columns = new ArrayList<>();
    Map<String,String> cloumn = new HashMap<>();
        cloumn.put("columnName","id");
        cloumn.put("columnKey","true");
        cloumn.put("dataType","int");
        cloumn.put("columnComment","主键");
        cloumn.put("extra","true");
    Map<String,String> cloumn1 = new HashMap<>();
        cloumn1.put("columnName","name");
        cloumn1.put("dataType","varchar");
        cloumn1.put("columnComment","名称");
        cloumn.put("extra","true");
        columns.add(cloumn);
        columns.add(cloumn1);
    File zipFile = new File("d:" + File.separator + "hello1.zip");

    ZipOutputStream zip = new ZipOutputStream(new FileOutputStream(
            zipFile));
    generatorCode(table,columns,zip);*/
    public static void main(String[] args) throws FileNotFoundException {
        ConfigurableApplicationContext applicationContext = SpringApplication.run(SpringBootXunTaApplication.class, args);
        System.out.println("=============start===============");
        genTable(applicationContext.getBean(JdbcTemplate.class));
        System.out.println("success!!!!!!!!!!!!!");
    }


    public static void genTable(JdbcTemplate jdbcTemplate, String... tableNames) throws FileNotFoundException {
        List<Map<String, Object>> allTables = getAllTables(jdbcTemplate, tableNames);
        for (Map<String, Object> allTable : allTables) {
            String tableName = allTable.get("table_name").toString();
            Map<String, String> table = new HashMap<>();
            table.put("tableName", tableName);
            table.put("tableComment", allTable.get("table_comment").toString());
            List<Map<String, String>> columns = getTableColumn(jdbcTemplate, tableName);
            System.out.println(columns.toString());
            File zipFile = new File("d:" + File.separator + "tables" + File.separator + tableName + ".zip");

            ZipOutputStream zip = new ZipOutputStream(new FileOutputStream(
                    zipFile));
            GenUtils.generatorCode(table, columns, zip);
        }
    }

    /**
     * 获取表名称
     *
     * @param jdbcTemplate
     * @param tableName
     * @return java.util.List<java.util.Map < java.lang.String, java.lang.Object>>
     * @author 不愿透露
     * @date 2021/7/29 14:56
     */
    public static List<Map<String, Object>> getAllTables(JdbcTemplate jdbcTemplate, String... tableName) {
        String tableAll = "select table_name,table_comment from information_schema.`TABLES` where table_schema='" + USE + "'";
        if (tableName.length > 0) {
            tableAll += " and table_name = '" + tableName[0] + "'";
        }
        List<Map<String, Object>> list = jdbcTemplate.queryForList(tableAll);
        return list;
    }

    /**
     * 获取表字段
     *
     * @param jdbcTemplate
     * @param tableName    表名称
     * @return java.util.List<java.util.Map < java.lang.String, java.lang.String>>
     * @author 不愿透露
     * @date 2021/7/29 14:53
     */
    public static List<Map<String, String>> getTableColumn(JdbcTemplate jdbcTemplate, String tableName) {
        List<Map<String, String>> columns = new ArrayList<>();
        String tableAll = "select column_name,column_type,extra,column_key,column_comment";
        tableAll += " from information_schema.`COLUMNS` where TABLE_SCHEMA='" + USE + "' and TABLE_NAME='" + tableName + "'";
        List<Map<String, Object>> list = jdbcTemplate.queryForList(tableAll);
        for (Map<String, Object> stringObjectMap : list) {
            Map<String, String> map = new HashMap<>();
            map.put("columnName", stringObjectMap.get("column_name").toString());
            map.put("dataType", columnToJava(stringObjectMap.get("column_type").toString()));
            if (!StringUtil.isNullOrEmpty(stringObjectMap.get("extra").toString())) {
                map.put("extra", columnToJava(stringObjectMap.get("extra").toString()));
            }
            if (!StringUtil.isNullOrEmpty(stringObjectMap.get("column_key").toString())) {
                map.put("columnKey", columnToJava(stringObjectMap.get("column_key").toString()));
            }
            if (!StringUtil.isNullOrEmpty(stringObjectMap.get("column_comment").toString())) {
                map.put("columnComment", columnToJava(stringObjectMap.get("column_comment").toString()));
            }
            columns.add(map);
        }
        return columns;
    }

    public static String columnToJava(String column) {
        if (!StringUtil.isNullOrEmpty(column)) {
            if (column.indexOf("varchar") > -1) {
                return column = "varchar";
            }
            if (column.indexOf("decimal") > -1) {
                return column = "decimal";
            }
        }
        return column;
    }

}
